/**
 * 将数字转换为科学计数法
 * @param num 数字
 * @returns {string}
 * @author 赵京涛
 */
export const useScientificNotation = (num) => {
  const absNum = Math.abs(Number(num))
  const exponent = 4
  const coefficient = absNum / Math.pow(10, exponent)
  return num === '' ? '' : coefficient.toFixed(2).replace(/\.?0+$/, '') + ' ✖ 10' + '⁴'
}

/**
 * 合并具有相同fieldCode值的字段信息
 * @param data
 * @returns {*[]}
 */
export const useMergeFieldInfo = (data) => {
  const mergedData = []
  const fieldCodeSet = new Set()
  data.forEach(item => {
    const fieldCode = item['fieldName2']
    if (fieldCode !== '') {
      if (fieldCodeSet.has(fieldCode)) {
        mergedData.forEach(mergedItem => {
          if (mergedItem['fieldName2'] === fieldCode) {
            mergedItem.list.push(item)
          }
        })
      } else {
        const temp = {
          fieldName2: fieldCode,
          list: [item]
        }
        mergedData.push(temp)
        fieldCodeSet.add(fieldCode)
      }
    } else {
      mergedData.push(item)
    }
  })
  return mergedData
}
